package com.yunb.pipeline;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

import java.util.List;

/**
 * 单机环境下使用pipeline
 * 不支持原子性，将多个命令一块执行，如果执行失败，会返回失败那个
 */
@Component
public class PipelineDemo {

    @Autowired
    private JedisPool jedisPool;

    public void pipelineCommand(){
        Jedis jedis = null;
        //******* 管道示例 ********
        //管道的命令执行方式：cat redis.txt | redis-cli -h 127.0.0.1 -a password - p 6379 --pipe
        try {
            jedis = jedisPool.getResource();

            Pipeline pl = jedis.pipelined();
            for (int i = 0; i < 10; i++) {
                pl.incr("pipelineKey");
                pl.set("zhuge" + i, "zhuge");
                //模拟管道报错
               // pl.setbit("zhuge", -1, true);
            }
            List<Object> results = pl.syncAndReturnAll();

            System.out.println(results);

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            jedis.close();
        }
    }
}
